﻿using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.API.Application.Common.Permission;
using RedDragon.RBAC.ErrorCodeEnum;
using SqlSugar;
using T.Common.Domain.Common;
namespace RedDragon.RBAC.API.Application.Common.Client
{
    public class DeleteClientHandler : IRequestHandler<DeleteClientCommand, Result>
    {
        private readonly ISqlSugarClient db;
        /// <summary>
        /// 权限仓储
        /// </summary>
        public readonly IBaseRepository<Clients> ClientsRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<DeleteClientHandler> logger;

        public DeleteClientHandler(ISqlSugarClient db, IBaseRepository<Clients> ClientsRepository, ILogger<DeleteClientHandler> logger)
        {
            this.db = db;
            this.ClientsRepository = ClientsRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(DeleteClientCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = await (await ClientsRepository.GetAll()).Where(d => d.ClientsId == request.ClientsId).FirstAsync();
                if (list != null)
                {
                    list.IsDel = 1;
                }
                var res = ClientsRepository.Update(list);
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "客户删除成功";
            }
            catch (Exception ex)
            {
                logger.LogError("客户删除异常");
                result.Msg = "客户删除异常";
            }
            return result;
        }

        /// <summary>
        /// 客户删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        //public async Task<Result> Handle(DeleteClientCommand request, CancellationToken cancellationToken)
        //{
        //    var result = new Result() { Code = (int)CommonErrorCode.Search };
        //    try
        //    {
        //        var list = await (await ClientsRepository.GetAll()).Where(d => d.ClientsId == request.ClientsId).FirstAsync();
        //        if (list != null)
        //        {
        //            list.IsDel = 1;
        //        }
        //        var res = ClientsRepository.Update(list);
        //        result.Code = (int)CommonErrorCode.Success;
        //        result.Msg = "客户删除成功";
        //    }
        //    catch (Exception ex)
        //    {
        //        logger.LogError("客户删除异常");
        //        result.Msg = "客户删除异常";
        //    }
        //    return result;

        //}

    }
}
